package com.czkt.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czkt.pojo.BasDict;
import com.czkt.pojo.CstCustomer;
import com.czkt.pojo.SysUser;
import com.czkt.service.IBasDictService;
import com.czkt.service.ICstCustomerService;
import com.czkt.utils.PageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    ICstCustomerService cstCustomerService;

    @Autowired
    IBasDictService basDictService;

    @RequestMapping("/list")
    public Object list(CstCustomer customer, @RequestParam(defaultValue = "1") Integer pageIndex, @RequestParam(defaultValue = "2") Integer pageSize, Model model) {
        IPage<CstCustomer> page = new Page<>(pageIndex, pageSize);

        QueryWrapper<CstCustomer> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.hasText(customer.getCustName()), CstCustomer::getCustName, customer.getCustName())
                .eq(StringUtils.hasText(customer.getCustRegion()), CstCustomer::getCustRegion, customer.getCustRegion());

        page = cstCustomerService.page(page, queryWrapper);

        PageUtils pageUtils = new PageUtils();

        BeanUtils.copyProperties(page, pageUtils);

        QueryWrapper<BasDict> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.lambda().eq(BasDict::getDictType, "地区");

        model.addAttribute("dictList", basDictService.list(queryWrapper1));
        model.addAttribute("page", pageUtils);
        model.addAttribute("custName", customer.getCustName());
        model.addAttribute("custRegion", customer.getCustRegion());

        return "customer/list";
    }

    @RequestMapping("/save")
    public Object save(CstCustomer customer){
        if (cstCustomerService.saveOrUpdate(customer)){
            return "redirect:customer/list";
        } else {
            return "redirect:customer/edit?customercId=" + customer.getCustNo();
        }
    }

    @RequestMapping("/edit")
    public Object edit(String customercId,Model model){

        model.addAttribute("customer",cstCustomerService.getById(customercId));

        QueryWrapper<BasDict> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.lambda().eq(BasDict::getDictType, "地区");
        model.addAttribute("areas", basDictService.list(queryWrapper1));

        QueryWrapper<BasDict> queryWrapper = new QueryWrapper<>();
        queryWrapper1.lambda().eq(BasDict::getDictType, "客户等级");
        model.addAttribute("customerLevel", basDictService.list(queryWrapper));

        return "customer/edit";
    }

    @RequestMapping("/del")
    @ResponseBody
    public Object del(String custNo){
        Map map = new HashMap<>();
        if (cstCustomerService.removeById(custNo)){
            map.put("delResult",true);
        }
        return map;
    }

}
